﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pdtransport_v2._0.Models;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace pdtransport_v2._0.Areas.back.Controllers
{
    public class nv_tk
    {
        public NhanVien nv;
        public TaiKhoan tk;
    };

    [Authorize(Roles = "admin")]
    public class NhanvienController : Controller
    {
        //
        // GET: /back/Nhanvien/

        pdtransportDataContext datacontext = new pdtransportDataContext();

        HelpController help = new HelpController();

        public ActionResult DanhSach(string keyword)
        {
            int total_rows = 0;
            List<nv_tk> nhanviens;
            if (keyword == null)
            {
                total_rows = (from _nv in datacontext.NhanViens
                              join _tk in datacontext.TaiKhoans
                                    on _nv.IDNV equals _tk.NhanVien
                              select new nv_tk { nv = _nv, tk = _tk }).Count();
            }
            else 
            {
                total_rows = (from _nv in datacontext.NhanViens
                              join _tk in datacontext.TaiKhoans
                                    on _nv.IDNV equals _tk.NhanVien
                              where (_nv.HoTen.Contains(keyword) == true || _tk.username.Contains(keyword) == true)
                              select new nv_tk { nv = _nv, tk = _tk }).Count();
            }

            ViewBag.total_row = total_rows;
            string base_URL = "nhanvien/danhsach"; // tên method của controller nhanvienController

            // phan trang
            string URL_segment;
            try
            {
                URL_segment = Request.Url.Segments[4];
            }
            catch (Exception)
            {
                URL_segment = null;
            }

            Libraries.Pagination pagination = new Libraries.Pagination(base_URL, URL_segment, total_rows);
            string pageLinks = pagination.GetPageLinks();
            int start = (pagination.CurPage - 1) * pagination.PerPage;
            int offset = pagination.PerPage;
            if (URL_segment != null)
                pageLinks = pageLinks.Replace(base_URL + "/", "");
            ViewBag.pageLinks = pageLinks;
            // phan trang

            if (keyword == null)
            {
                nhanviens = (from _nv in datacontext.NhanViens
                              join _tk in datacontext.TaiKhoans
                                    on _nv.IDNV equals _tk.NhanVien
                              select new nv_tk { nv = _nv, tk = _tk }).Skip(start).Take(offset).ToList();
            }
            else
            {
                nhanviens = (from _nv in datacontext.NhanViens
                              join _tk in datacontext.TaiKhoans
                                    on _nv.IDNV equals _tk.NhanVien
                              where (_nv.HoTen.Contains(keyword) == true || _tk.username.Contains(keyword) == true)
                             select new nv_tk { nv = _nv, tk = _tk }).Skip(start).Take(offset).ToList();
            }

            ViewBag.total_rows = total_rows;
            ViewBag.nhanviens = nhanviens;
            return View();
        }

        [HttpGet]
        public ActionResult Them()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Them(string manv, string hoten, string diachi, string sodt, string cmnd, string username, string password)
        {
            NhanVien nv = new NhanVien();
            nv.MaNV = manv;
            nv.HoTen = hoten;
            nv.DiaChi = diachi;
            nv.SoDT = sodt;
            nv.CMND = cmnd;
            nv.VaiTro = "manager";
            nv.TinhTrang = 1;

            datacontext.NhanViens.InsertOnSubmit(nv);
            datacontext.SubmitChanges();

            int idnv = datacontext.NhanViens.OrderByDescending(_nv => _nv.IDNV).Take(1).Single().IDNV;

            TaiKhoan tk = new TaiKhoan();
            tk.NhanVien = idnv;
            tk.username = username;
            tk.VaiTro = "manager";
            tk.TinhTrang = 1;
            tk.password = help.Encrypt(password);

            datacontext.TaiKhoans.InsertOnSubmit(tk);
            datacontext.SubmitChanges();

            ViewBag.Them = 1;
            return RedirectToAction("DanhSach");
        }

        [HttpGet]
        public ActionResult Sua(int id)
        {
            nv_tk nv = (from _nv in datacontext.NhanViens
                        join _tk in datacontext.TaiKhoans on _nv.IDNV equals _tk.NhanVien
                        where _nv.IDNV == id
                        select new nv_tk { nv = _nv, tk = _tk }).Single();
            ViewBag.Nhanvien = nv;
            return View();
        }

        [HttpPost]
        public ActionResult Sua(int idnv, string hoten, string diachi, int TinhTrang, string cmnd, string password)
        {
            NhanVien nv = (from _nv in datacontext.NhanViens
                           where _nv.IDNV == idnv
                           select _nv).Single();
            nv.HoTen = hoten;
            nv.DiaChi = diachi;
            nv.TinhTrang = TinhTrang;
            nv.CMND = cmnd;

            TaiKhoan tk = (from _tk in datacontext.TaiKhoans
                           where _tk.NhanVien == idnv
                           select _tk).Single();

            if (password.Length != 0) // nếu password có thay đổi thì mới cập nhật
            {
                tk.password = help.Encrypt(password);
            }
            tk.TinhTrang = TinhTrang;

            ViewBag.Sua = 1;

            datacontext.SubmitChanges();
            nv_tk __nv = (from _nv in datacontext.NhanViens
                        join _tk in datacontext.TaiKhoans on _nv.IDNV equals _tk.NhanVien
                        where _nv.IDNV == idnv
                        select new nv_tk { nv = _nv, tk = _tk }).Single();
            ViewBag.Nhanvien = __nv;
            return View();
        }

        public ActionResult Xoa(int id)
        {
            NhanVien nv = (from _nv in datacontext.NhanViens
                           where _nv.IDNV == id
                           select _nv).Single();

            TaiKhoan tk = (from _tk in datacontext.TaiKhoans
                           where _tk.NhanVien == id
                           select _tk).Single();

            nv.TinhTrang = 0;
            tk.TinhTrang = 0;

            datacontext.SubmitChanges();

            ViewBag.Xoa = 1;
            return RedirectToAction("Danhsach");
        }
    }

}
